package ws.jdbc.concurrent.driver.sqlserver

import java.sql.ResultSet

/**
 *
 * @author Wilson Santos, 36228 <wilson.santos@ua.pt>
 */
class CStatementTest extends GroovyTestCase {
  String url = "jdbc:sqlserver://goldberg-wm:1433;username=sa;password=sg4765j;database=JDBCDriverTests"

  void testExecuteBatch() {
    println "testExecuteBatch"

    def stmt = new CConnection(url, null).createStatement()
    stmt.addBatch("INSERT INTO Table1 VALUES(1, 'new val1')")
    stmt.addBatch("INSERT INTO Table1 VALUES(2, 'new val2')")
    stmt.addBatch("INSERT INTO Table1 VALUES(3, 'new val3')")
    stmt.addBatch("UPDATE Table1 SET Col2='updated' WHERE Col2 LIKE 'new%'")

    def ret = stmt.executeBatch()
    [1, 1, 1, 3].eachWithIndex { it, index ->
      assertEquals it, ret[index]
    }
  }

  void UpdateRow() {
    println "testUpdateRow()"

    def con = new CConnection(url, null)
    def stmt = con.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE)

    def rs =  stmt.executeQuery("SELECT * FROM Table4")
    rs.next()
    rs.updateDate(1, new java.sql.Date(System.currentTimeMillis()))
    rs.insertRow()
  }

  void UpdateRowFloat() {
    println "testUpdateRowFloat()"

    def con = new CConnection(url, null)
    def stmt = con.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE)

    def rs =  stmt.executeQuery("SELECT * FROM Table2")
    rs.next()
    rs.updateDouble(1, 9.99)
    rs.insertRow()
  }

  void ExecuteQuery() {
    println "testExecuteQuery()"

    def con = new CConnection(url, null)
    def stmt = con.createStatement()

    def rs = stmt.executeQuery("SELECT * FROM Table2")
    rs.next()
    println "f=" + rs.getDouble(1)
  }

  void ExecuteQuery2() {
    println "testExecuteQuery()"

    def con = new CConnection(url, null)
    def stmt = con.createStatement()

    def rs =  stmt.executeQuery("SELECT * FROM Table4")
    rs.next()
    println "d=" + new java.util.Date(rs.getDate(1).getTime())
  }

  void ExecuteUpdate() {
    println "testExecuteUpdate()"

    def con = new CConnection(url, null)
    def stmt = con.createStatement()

    println "count=" + stmt.executeUpdate("INSERT INTO Table1 VALUES(999, 'KKK')")
    println "count=" + stmt.executeUpdate("UPDATE Table1 SET Col2 = 'XXX' WHERE Col1 > 90")
  }
}

